/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package com.xjrsoft.module.bi.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.utils.UploadUtil;
import com.xjrsoft.module.bi.common.constant.LauncherConstant;
import com.xjrsoft.module.bi.common.tool.Condition;
import com.xjrsoft.module.bi.common.tool.Func;
import com.xjrsoft.module.bi.common.tool.Kv;
import com.xjrsoft.module.bi.common.tool.Query;
import com.xjrsoft.module.bi.log.DataLog;
import com.xjrsoft.module.bi.pojo.dto.VisualDTO;
import com.xjrsoft.module.bi.pojo.entity.BiFile;
import com.xjrsoft.module.bi.pojo.entity.Visual;
import com.xjrsoft.module.bi.pojo.entity.VisualCategory;
import com.xjrsoft.module.bi.service.IVisualCategoryService;
import com.xjrsoft.module.bi.service.IVisualService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * 可视化模块 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@RequestMapping(LauncherConstant.APPLICATION_VISUAL_NAME + "/visual")
@Api(value = "可视化模块")
public class VisualController {

	private final IVisualService visualService;
	private final IVisualCategoryService categoryService;

	/**
	 * 详情
	 */
	@DataLog("大屏详情")
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情")
	public R detail(@RequestParam Long id) {
		VisualDTO detail = visualService.detail(id);
		return R.ok(detail);
	}

	/**
	 * 分页 可视化表
	 */
	@DataLog("大屏列表")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R list(String title, Integer category, Query query) {
		IPage<Visual> pages = visualService.page(Condition.getPage(query),
			Condition.getQueryWrapper(Kv.create().set("title", title).set("category", category), Visual.class));
		return R.ok(pages);
	}

	/**
	 * 新增 可视化表
	 */
	@DataLog("大屏新增")
	@PostMapping("/save")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "新增")
	public R save(@Valid @RequestBody VisualDTO visual) {
		boolean temp = visualService.saveVisual(visual);
		if (temp) {
			Long id = visual.getVisual().getId();
			return R.ok(Kv.create().set("id", String.valueOf(id)));
		} else {
			return R.ok(false);
		}
	}

	/**
	 * 修改 可视化表
	 */
	@DataLog("大屏修改")
	@PostMapping("/update")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "修改")
	public R update(@Valid @RequestBody VisualDTO visual) {
		return R.ok(visualService.updateVisual(visual));
	}


	/**
	 * 删除 可视化表
	 */
	@DataLog("大屏删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "逻辑删除")
	public R remove(@RequestParam String ids) {
		return R.ok(visualService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 复制 可视化表
	 */
	@DataLog("大屏复制")
	@PostMapping("/copy")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "复制")
	public R copy(@RequestParam Long id) {
		return R.ok(visualService.copyVisual(id));
	}

	/**
	 * 获取分类
	 */
	@GetMapping("category")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "获取类型")
	public R category() {
		List<VisualCategory> list = categoryService.list();
		return R.ok(list);
	}

	/**
	 * 上传文件
	 */
	@DataLog("资源上传")
	@SneakyThrows
	@PostMapping("/put-file")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "上传")
	public R putFile( @RequestParam MultipartFile file) {
		String s = UploadUtil.uploadFile(file);
		BiFile file1 = new BiFile();
		file1.setDomain("xxxx");
		file1.setName(file.getName());
		file1.setOriginalName(file.getName());
		file1.setLink(s);
		return R.ok(file1);
	}

}
